<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Evaluasi extends CI_Controller {

    function __construct() 
	{
        parent::__construct();
        $this->load->library('flexigrid');
        $this->load->library('form_validation');
        $this->load->helper('flexigrid');
        $this->load->helper(array('form', 'url'));
        $this->load->model('evaluasi_model');
		$this->load->model('proyek_model');
		$this->load->model('user_model');
		$this->cek_session();
    }

    //load Grid	
	function cek_session()
	{	
		$kode_role = $this->session->userdata('role_id');
		if($kode_role == '')
		{
			redirect('login');
		}
	}
	
	public function index()
	{
		$this->grid();
	}
	
	public function grid()
	{
		$kode_role = $this->session->userdata('role_id');		
		$colModel['no'] = array('No', 30, TRUE, 'center', 0);
		$colModel['NAMA_EVALUASI'] = array('Nama evaluasi', 180, TRUE, 'center', 1);
        $colModel['TANGGAL_MULAI_EVALUASI'] = array('Tanggal Mulai', 85, TRUE, 'center', 1);
		$colModel['TANGGAL_SELESAI_EVALUASI'] = array('Tanggal Selesai', 90, TRUE, 'center', 1);
		$colModel['NAMA_PROYEK'] = array('Nama Proyek', 200, TRUE, 'center', 1);
		$colModel['AKTIF'] = array('Status', 45, TRUE, 'center', 1);
        $colModel['KETERANGAN'] = array('Keterangan', 220, TRUE, 'center', 1);
		if($kode_role == 2 || $kode_role == 4) $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);
		$colModel['Lihat_Data_Aktifitas'] = array('Data Aktifitas', 100, TRUE, 'center', 0);			        
			
		//setting konfigurasi pada bottom tool bar flexigrid
		$gridParams = array(
                'width' => 'auto',
                'height' => 300,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
                'title' => 'Daftar evaluasi',
                'showTableToggleBtn' => true
            );
							
		//menambah tombol pada flexigrid top toolbar
		if($kode_role == 2 || $kode_role == 4) 
		{			
            $buttons[] = array('tambah', 'add', 'spt_js');
            $buttons[] = array('separator');
            $buttons[] = array('hapus', 'delete', 'spt_js');
			$buttons[] = array('separator');
		}
		else
		{
			$buttons = null;
		}
				            		
		// mengambil data dari file controler ajax pada method grid_user				
			$grid_js = build_grid_js('flex1', site_url("evaluasi/grid_foto"), $colModel, 'NAMA_EVALUASI', 'asc', $gridParams, $buttons);

            $data['added_js'] =
                    "<script type='text/javascript'>
        function spt_js(com,grid)
        {
                if (com=='tambah')
                {
                        location.href='" . base_url() . "index.php/evaluasi/form';
                }

                if (com=='hapus')
                        {
                           if($('.trSelected',grid).length>0){
                                   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' evaluasi?')){
                                                var items = $('.trSelected',grid);
                                                var itemlist ='';
                                                for(i=0;i<items.length;i++){
                                                        itemlist+= items[i].id.substr(3)+',';
                                                }
                                                $.ajax({
                                                   type: 'POST',
                                                   url: '" . base_url() . "index.php/evaluasi/delete" . "',
                                                   data: 'items='+itemlist,
                                                   success: function(data){
                                                        $('#flex1').flexReload();
														alert(data);
                                                   }
                                                });
                                        }
                                } else {
                                        return false;
                                }
                        }

        }
		
		
        </script>
        ";

            $data['js_grid'] = $grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);				
	}

    function grid_foto() 
	{
		$kode_role = $this->session->userdata('role_id');		
        $valid_fields = array('NAMA_EVALUASI', 'TANGGAL_MULAI_EVALUASI', 'TANGGAL_SELESAI_EVALUASI', 'NAMA_PROYEK', 'KETERANGAN');
        $this->flexigrid->validate_post('NAMA_EVALUASI', 'asc', $valid_fields);
		$records = $this->evaluasi_model->get_data_evaluasi_proyek();
		$this->output->set_header($this->config->item('json_header'));	
		
        $no = 0;
		
        foreach ($records['records']->result() as $row) 
		{
			$link = '';
			$nama_evaluasi = str_replace("\'", "'", $row->NAMA_EVALUASI);
			
			if ($row->AKTIF == 1) 
			{
				$status_aktif = 'check.png';				
			}
			else{
				$status_aktif = 'unaktif.png';				
				$link = '<a href=\'' . base_url() . 'index.php/evaluasi/edit/' . $row->ID_EVALUASI . '\'>';
			}					
			
			if($kode_role == 2 || $kode_role == 4 )
			{
				$record_items[] = array(
				$row->ID_EVALUASI,
				$no = $no + 1,
				$nama_evaluasi,
				$row->TANGGAL_MULAI_EVALUASI,
				$row->TANGGAL_SELESAI_EVALUASI,	
				$row->NAMA_PROYEK,	
				'<img border=\'0\' src=\'' . base_url() . 'images/grid/'.$status_aktif.'\'></a> ',
				$row->KETERANGAN,
				''.$link.'<img border=\'0\' src=\'' . base_url() . 'images/grid/edit.png\'></a> ',				
				'<a href=\'' . base_url() . 'index.php/detail_aktifitas/index/' . $row->ID_EVALUASI . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/folder.png\'></a> '
				);									
			} else if($kode_role == 3)
			{
				$record_items[] = array(
				$row->ID_EVALUASI,
				$no = $no + 1,
				$nama_evaluasi,
				$row->TANGGAL_MULAI_EVALUASI,
				$row->TANGGAL_SELESAI_EVALUASI,	
				$row->NAMA_PROYEK,	
				'<a href=\'' . base_url() . 'index.php/evaluasi/update_status_aktif/' . $row->ID_EVALUASI . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/'.$status_aktif.'\'></a> ',
				$row->KETERANGAN,				
				'<a href=\'' . base_url() . 'index.php/detail_aktifitas/index/' . $row->ID_EVALUASI . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/folder.png\'></a> '
				);
			}							                       
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

    function form() 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $data['status'] = 'new';
            $data['failed'] = false;
			$data['proyek'] = $this->proyek_model->get_data_aktif_proyek();
            $data['aa'] = '';
            $data['content'] = $this->load->view('form_evaluasi', $data, true);
            $this->load->view('main', $data);
        }
    }

	function upload() 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            if ($this->cek_validasi() == FALSE) 
			{
                $this->form();
            } else {				
                $data = array(
                        'NAMA_EVALUASI' => $this->input->post('NAMA_EVALUASI'),
						'TANGGAL_MULAI_EVALUASI' => $this->input->post('TANGGAL_MULAI_EVALUASI'),
						'TANGGAL_SELESAI_EVALUASI' => $this->input->post('TANGGAL_SELESAI_EVALUASI'),
						'ID_PROYEK' => $this->input->post('ID_PROYEK'),
						'KETERANGAN' => $this->input->post('KETERANGAN')
                );
				
				$data = str_replace("'", "\'", $data);
				$this->evaluasi_model->insert($data);
				redirect('evaluasi');
            }
        }
    }

    public function edit($id) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $data['status'] = 'edit';
            $record_artikel = $this->evaluasi_model->selectone($id);
            foreach ($record_artikel->result() as $artikel) {
                $data['ID_EVALUASI'] = $artikel->ID_EVALUASI;
                $data['NAMA_EVALUASI'] = $artikel->NAMA_EVALUASI;
                $data['TANGGAL_MULAI_EVALUASI'] = $artikel->TANGGAL_MULAI_EVALUASI;
				$data['TANGGAL_SELESAI_EVALUASI'] = $artikel->TANGGAL_SELESAI_EVALUASI;
				$data['KETERANGAN'] = $artikel->KETERANGAN;
				$data['ID_PROYEK'] = $artikel->ID_PROYEK;              
            }
            $data['failed'] = false;
            $data['aa'] = '';
			$data = str_replace("\'", "'", $data);
			$data['content'] = $this->load->view('form_evaluasi', $data, true);
            $this->load->view('main', $data);
        }
    }

    function update($id) 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            if ($this->cek_validasi() == FALSE) 
			{
                $this->edit($id);
            } else {																		
					$data = array(
						'NAMA_EVALUASI' => $this->input->post('NAMA_EVALUASI'),
						'TANGGAL_MULAI_EVALUASI' => $this->input->post('TANGGAL_MULAI_EVALUASI'),
						'TANGGAL_SELESAI_EVALUASI' => $this->input->post('TANGGAL_SELESAI_EVALUASI'),
						'KETERANGAN' => $this->input->post('KETERANGAN'),
						'ID_PROYEK' => $this->input->post('ID_PROYEK')						
						);
					$data = str_replace("'", "\'", $data);
                    $this->evaluasi_model->update($id, $data);
                    redirect('evaluasi');
            }
        }
    }
	
		
	function update_status_aktif($ID) 
	{
		$kode_role = $this->session->userdata('role_id');
        if ($kode_role == 3) 
		{
			$data = array(
						'AKTIF' => 1       
                    );
					$data = str_replace("'", "\'", $data);
                    $this->evaluasi_model->update($ID, $data);
                    redirect('evaluasi');
            
        } else {           
                    redirect('login');
        } 
    }
	
	
	function delete() 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $spt_ids_post_array = explode(",", $this->input->post('items'));
            foreach ($spt_ids_post_array as $id)
			{
				//cek data sdm
				if (isset($id) && $id != '') {
					$sql = $this->db->query("SELECT SUM(AKTIF) AS CEK FROM evaluasi WHERE ID_EVALUASI = ".$id.";");
					$cek = '';
					foreach ($sql->result() as $rows)
					{
						$cek = $rows->CEK;
					}
							
					$msg='';
					if($cek > 0)
					{
						// $this->sk_model->delete($id);
						// $msg ='Data yang Anda pilih berhasil dihapus.';
						$msg ='Proses hapus gagal. Data Evaluasi sudah disetujui.';
					}
					else
					{
						$this->evaluasi_model->delete($id);
						$msg ='Data yang Anda pilih berhasil dihapus.';
					}
					$this->output->set_header($this->config->item('ajax_header'));
					$this->output->set_output($msg);
				}
			}	
			
        }
    }
	
	
	function generate_daftar_aktifitas($id_evaluasi){
		$record_artikel = $this->evaluasi_model->selectone($id_evaluasi);
		foreach ($record_artikel->result() as $artikel) {
			$data['ID_EVALUASI'] = $artikel->ID_EVALUASI;
			$data['NAMA_EVALUASI'] = $artikel->NAMA_EVALUASI;
			$data['TANGGAL_MULAI_EVALUASI'] = $artikel->TANGGAL_MULAI_EVALUASI;
			$data['TANGGAL_SELESAI_EVALUASI'] = $artikel->TANGGAL_SELESAI_EVALUASI;
			$data['KETERANGAN'] = $artikel->KETERANGAN;
		}
		
		$data['data_buku_l'] = $this->evaluasi_model->get_data_buku_l($id_evaluasi);
		$data['data_buku_p'] = $this->evaluasi_model->get_data_buku_p($id_evaluasi);
		$this->load->view('buku_santri', $data);
	}
	
	function alpha_space($str)
    {
        return ( ! preg_match("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ", $str)) ? FALSE : TRUE;
    } 

    function cek_validasi() {
        // Setting Rules
        $this->form_validation->set_rules('NAMA_EVALUASI', 'Nama Evaluasi', 'required');
		$this->form_validation->set_rules('TANGGAL_MULAI_EVALUASI', 'Tanggal Mulai Evaluasi', 'required');
		$this->form_validation->set_rules('TANGGAL_SELESAI_EVALUASI', 'Tanggal Selesai Evaluasi', 'required');
		$this->form_validation->set_rules('KETERANGAN', 'Keterangan', 'required');
		
        //Setting Error Message
        $this->form_validation->set_message('required', 'Field %s harus diisi.');
        $this->form_validation->set_message('numeric', 'Field %s harus diisi angka.');
		$this->form_validation->set_message('greater_than', 'Field %s harus diisi angka lebih dari 0');
		//$this->form_validation->set_message('alpha_space', 'Field %s harus diisi dengan huruf dan spasi');
		
        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }

}
